<?php
require '../script/mysqlconfig.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // 获取用户输入的用户名和密码
    $username = $_POST['username'];
    $password = $_POST['password'];
    echo $username;
    echo "<br>";
    echo $password;
    echo "<br>";
    $conn = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
    $pass_hash = password_hash($password,PASSWORD_DEFAULT);
    echo "根据输入的密码转为加密密码:",$pass_hash;echo "<br>";

    $sql = "INSERT INTO users (name,password,info) VALUES ('$username','$pass_hash','$password')";
    echo "输出插入数据库的语句:",$sql;
    echo "<br>";
    $conn->query($sql);

    echo "开始进行SQL查询<br>";

    /* 判断用户是否点击了登录按钮*/
    if(isset($_POST["login"])){
        $sql = "SELECT password FROM users WHERE name='$username'";
        $res =$conn->query($sql)->fetch_assoc();
        /**登录验证 */
        if($res){
            if(password_verify($password,$res['password'])){
                echo "登录成功！";
            }else{
                echo "登录失败1";
            }
        }else{
            echo "登录失败2!";
        }
    }
}

?>
<form action="" method="POST">
    <div>
        <label for="name">用户名：</label>
        <input type="text" id="name" name="username" placeholder="请输入用户名" required>
    </div>
    <div>
        <label for="pass">密 &nbsp;&nbsp;&nbsp; 码:</label>
        <input type="password" name="password" id="pass" placeholder="请输入密码" required>
    </div>
    <input type="submit" name="login" value="登录">
</form>
